package com.polaris.live.widget.task

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.RelativeLayout
import com.blankj.utilcode.util.StringUtils
import com.polaris.live.R.*
import com.polaris.live.databinding.WidgetLoginTaskBinding
import com.polaris.live.dialog.LoginRewardDialog
import com.polaris.live.dialog.common.TipsDialog
import com.polaris.live.resp.back_resp.DailyTask
import com.polaris.live.resp.back_resp.DailyTaskVo
import com.polaris.live.translation.R

/**
 * LoginTaskView登录奖励view
 * @author Created by 天晴 on 2024/1/24 19:17
 * @since 1.0.0
 **/
class LoginTaskView(context: Context, attributeSet: AttributeSet) :
    RelativeLayout(context, attributeSet) {

    private val mBinding = WidgetLoginTaskBinding.inflate(LayoutInflater.from(context), this)

    init {
        mBinding.whatBtn.setOnClickListener {
            TipsDialog.show(
                context,
                StringUtils.getString(R.string.login_award),
                "${StringUtils.getString(R.string.login_rewards_task_tips1)}" +
                        "\n${StringUtils.getString(R.string.login_rewards_task_tips2)}" +
                        "\n${StringUtils.getString(R.string.login_rewards_task_tips3)}"
            )

        }
    }

    fun setData(dailyTask: DailyTaskVo) {
        visibility = if (dailyTask.tasks.isNotEmpty()) View.VISIBLE else View.GONE

        dailyTask.tasks.forEach { task ->
            val day = task.day
            val rewardTitle = task.rewardTitle
            val status = task.status

            setDayTask(task, day, rewardTitle, status)
        }
    }

    private fun setDayTask(dailyTask: DailyTask, day: Long, rewardTitle: String, status: Long) {
        val textView = when (day) {
            1L -> mBinding.dayTv1
            2L -> mBinding.dayTv2
            3L -> mBinding.dayTv3
            4L -> mBinding.dayTv4
            5L -> mBinding.dayTv5
            6L -> mBinding.dayTv6
            7L -> mBinding.dayTv7
            else -> return
        }

        val imageView = when (day) {
            1L -> mBinding.dayImg1
            2L -> mBinding.dayImg2
            3L -> mBinding.dayImg3
            4L -> mBinding.dayImg4
            5L -> mBinding.dayImg5
            6L -> mBinding.dayImg6
            7L -> mBinding.dayImg7
            else -> return
        }

        val statusView = when (day) {
            1L -> mBinding.dayStatus1
            2L -> mBinding.dayStatus2
            3L -> mBinding.dayStatus3
            4L -> mBinding.dayStatus4
            5L -> mBinding.dayStatus5
            6L -> mBinding.dayStatus6
            7L -> mBinding.dayStatus7
            else -> return
        }

        textView.text = rewardTitle
        when (status) {
            0L -> { // 进行中
                imageView.setImageResource(if (day == 7L) drawable.task_box_neck_1 else drawable.task_box_neck)
                statusView.visibility = View.GONE
            }

            1L -> { // 可领取
                imageView.setImageResource(if (day == 7L) drawable.task_box_neck_1 else drawable.task_box_neck)
                statusView.visibility = View.GONE
            }

            2L -> { // 已领取
                imageView.setImageResource(if (day == 7L) drawable.task_box_open_1 else drawable.task_box_open)
                statusView.visibility = View.VISIBLE
            }
        }

        imageView.setOnClickListener {
            LoginRewardDialog.show(context, dailyTask)

        }
    }

}